package cn.bdqn.fin.service.impl;

import cn.bdqn.fin.mapper.InvoiceMapper;
import cn.bdqn.fin.model.Invoice;
import cn.bdqn.fin.model.InvoiceExample;
import cn.bdqn.fin.service.InvoiceService;
import cn.bdqn.model.EUList;
import cn.bdqn.model.PageBean;
import cn.bdqn.model.R;
import cn.bdqn.service.BaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gurilla
 * @create 2019-08-23 15:39
 */
@Service
public class InvoiceServiceImpl extends BaseService implements InvoiceService {
    @Resource
    private InvoiceMapper invoiceMapper;

    @Override
    public EUList<Invoice> list(PageBean pageBean, String taxNo) {
        InvoiceExample example = new InvoiceExample();
        InvoiceExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(taxNo)){
            criteria.andTaxNoLike("%"+ taxNo + "%");
        }
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        List<Invoice> invoices = invoiceMapper.selectByExample(example);
        PageInfo<Invoice> info=new PageInfo<>(invoices);
        return new EUList<>(info.getTotal(),info.getList());
    }

    @Override
    public R save(Invoice invoice) {
        if(invoice.getId()==null){
            invoice.setInvoiced(false);
            invoice.setOpid(getUserid());
            invoiceMapper.insertSelective(invoice);
        }else{
            invoice.setOpid(getUserid());
            invoiceMapper.updateByPrimaryKeySelective(invoice);
        }
        return R.ok();
    }
}
